<template>
 <el-dialog 
   width="80%" 
  :close-on-click-modal="false"
  :title="title"
  :model-value="isShow"
  @open="onOpen">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="66px">
      <el-form-item label="用户名" prop="loginName">
        <el-input v-model="form.loginName" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="form.nickName" />
      </el-form-item>

      <el-form-item label="头像" prop="avatar">
        <el-input v-model="form.avatar" />
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-input v-model="form.sex" />
      </el-form-item>

      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>

      <el-form-item style="margin-bottom: 0;" label="机构" prop="tenantId">
        <el-select
                v-model="form.tenantId"
                style="width: 437px"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in tenants"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button  @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onSave">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDetail, add, save } from '@/api/admin/user'
import { getAll } from '@/api/admin/tentant'

export default {
  props: {
    data: {
      required: true,
      type: String,
      default: ''
    },
    isShow: {
      require: true,
      type: Boolean,
      default: false
    },
    title: {
      require: true,
      type: String,
      default: ""
    }
  },
  emits: ['onChange','update:isShow'],
  data() {
    return {
      form: {},
      rules: {},
      tenants:[]
    }
  },
  mounted() {
      getAll({}).then((res) => {
        this.tenants = res.result
      })
  },
  methods: {
    async onOpen() {
      if(this.data) {
        const user = await getDetail(this.data)
        this.form = user.result
        // this.form.roles = user.data.roles
      }
      else {
        this.form = {
          loginName:'',
          nickName: '',
          avatar: '',
          sex: '',
          phone: '',
          email: '',
          tenantId: ''
        }
      }
      
    },
    async onSave() {
      if(this.data) {
        await save(this.form)
      }
      else {
        await add(this.form)
      }
      this.$emit("onChange", this.form)
      this.$emit("update:isShow", false)
    },
    onCancel() {
      this.$emit("update:isShow", false)
    },
    onClose() {

    }
  },
  watch: {
    'data': async function() {
      
    }
  }
}
</script>


